package cn.sjtu.java_util_concurrent.completable_future;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

/**
 * @author syo 2319615164@qq.com
 * @description 工具包
 * @date 2024/5/14
 * @Copyright syo
 */
public class CommonUtils {
    public static String readFile(String path) {

        try {
            byte[] bytes = Files.readAllBytes(Paths.get(path));
            return new String(bytes, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void sleep(long millis, TimeUnit unit) {
        try {
            unit.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void sleep(long millis) {
        try {
            TimeUnit.SECONDS.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void log(String msg) {
        LocalTime now = LocalTime.now();
        long id = Thread.currentThread().getId();
        String name = Thread.currentThread().getName();
        System.out.println(now + " | " + id + " | " + name + " | " + msg);
    }

    public static void main(String[] args) {
        CommonUtils.log("hello world");
        CommonUtils.sleep(2);
        CommonUtils.sleep(2, TimeUnit.SECONDS);
        CommonUtils.log("hello world");
        String s = CommonUtils.readFile("src/main/java/cn/sjtu/juc/completable_future/content.txt");
        System.out.println(s);
    }
}
